﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using CoralNode.Core.Entity.Domains.Node;
using CoralNode.Core.Entity.Model;
using CoralNode.MvcClient.Helper;
using NHibernate.Event;
using NHibernate.Mapping;

namespace Gongfu.Helper
{
    public class SortHelper
    {
        public static string SortByRank(long id,string group)
        {
            ComponentHelper component = new ComponentHelper();
            var categroryId = group == "少儿组" ? 4 : 5;
            IList<NodeDomain> workList = component.ListNode(categroryId);
            List<NodeDomain> resultList = workList.ToList();
            for (int i = 0; i < resultList.Count; i++)
            {
                if (resultList[i].Id == id)
                {
                    return (i + 1).ToString();
                }
            }
            return "0";
        }

        public static IList<NodeDomain> RageListByRecommend(long categoryId, int page,int size)
        {
            ComponentHelper component = new ComponentHelper();
            IList<NodeDomain> totaList = component.ListNode(categoryId,0,null);
            NodeDomain[] totalArray = totaList.ToArray();
            var resultList = SortByRecommend(totalArray);
            return resultList.Skip((page - 1) * size).Take(size).ToList();
        }
        public static IList<NodeDomain> SortByRecommend(NodeDomain[] arrayNode)
        {
            if (arrayNode.Length > 0)
            {
                QuickSort(arrayNode, 0, arrayNode.Length - 1);
            }
            return arrayNode.ToList();
        }
        public static void QuickSort(NodeDomain[] array, int low, int high)
        {
            if (low < high)
            {
                int n = Partition(array, low, high);
                QuickSort(array, low, n);
                QuickSort(array, n + 1, high);
            }
        }
        public static int Partition(NodeDomain[] array, int low, int high)
        {
            int pivot = int.Parse(array[low].Field.RecommendVote); 
            while (low < high)
            {
                while (low < high && int.Parse(array[high].Field.RecommendVote) <= pivot)
                {
                    --high;
                }
                var temp = array[low];
                array[low] = array[high];
                array[high] = temp;
                while (low < high && int.Parse(array[low].Field.RecommendVote) >= pivot)
                {
                    ++low;
                }
                var temp2 = array[low];
                array[low] = array[high];
                array[high] = temp2;
            }
            return low;
        }

        public static void Swap(NodeDomain a, NodeDomain b)
        {
            NodeDomain t = new NodeDomain();
            t = a;
            a = b;
            b = t;
        }
    }
}